package com.lagou.product.controller;

import com.lagou.common.pojo.Products;
import com.lagou.common.pojo.ProductsDTO;
import com.lagou.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Ethan
 * @date 2021/03/02 15:31
 * @description 商品服务控制器
 */

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 根据商品ID查询商品信息
     * @param id    商品ID
     * @return  查询到的商品对象
     */
    @GetMapping("/{id}")
    public Products queryById(@PathVariable Integer id) {
        return productService.queryById(id);
    }

    /**
     * 根据商品ID删除商品信息
     * @param id    商品ID
     * @return  受影响的行数，1为删除成功
     */
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Integer id) {
        int i = productService.deleteById(id);
        if (i == 1) {
            return "删除成功！";
        }
        return "删除失败！";
    }

    @PutMapping("/update")
    public String updateById(@RequestBody Products product) {
        int i = productService.updateById(product);
        if (i == 1) {
            return "修改成功！";
        }
        return "修改失败！";
    }

    /**
     * 根据条件分页查询商品信息
     * @param productsDTO   包含查询条件的商品传输对象
     * @return  查询到的Map对象
     */
    @PostMapping("/queryByPage")
    public Map<String, Object> queryByPage(@RequestBody ProductsDTO productsDTO) {
        return productService.queryByPage(productsDTO);
    }
}
